﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TankBattle.Models
{
    public class PortralGate
    {
        #region Variable
        protected MultiSprite gateNormal;
        protected MultiSprite gateReverse;
        protected Texture2D textureNormal;
        protected Texture2D textureReverse;
        protected Random random;
        protected bool first;
        #endregion 

        #region Construction 
        public PortralGate()
        { }
        public PortralGate(Texture2D textureNormal, Texture2D textureReverse)
        {
            Initallize(textureNormal, textureReverse);
        }
        #endregion 

        #region Properties
        public  Rectangle PolygonNormal(int i)
        {
            return new Rectangle(
                    (int)(gateNormal.Positions[i].X + Constant.PORTRAL_RADIUS),
                    (int)(gateNormal.Positions[i].Y + Constant.PORTRAL_RADIUS),
                    Constant.PORTRAL_WIDTH -  Constant.PORTRAL_RADIUS * 2,
                    Constant.PORTRAL_HEIGHT - Constant.PORTRAL_RADIUS * 2
                    );
        }
        #endregion

        #region Main Function 
        protected  void Initallize(Texture2D textureNormal, Texture2D textureReverse)
        {
            first = true;
            gateNormal = new MultiSprite(true);
            this.textureNormal = textureNormal;

            gateReverse = new MultiSprite(true);
            this.textureReverse = textureReverse;
            
            random = new Random(DateTime.Now.Millisecond);

        }
        public  bool Update()
        {
            gateNormal.Update();
            gateReverse.Update();
            return true;
        }
        public  void Draw(SpriteBatch spriteBatch)
        {
            gateNormal.Draw(spriteBatch);
            gateReverse.Draw(spriteBatch);
        }
        public virtual void Action(ref List<GameObject> objects)
        {
            for (int i = 0; i < gateNormal.Positions.Count; i++)
                foreach (GameObject obj in objects)
                {
                    if (PolygonNormal(i).Intersects(obj.Polygon))
                    {
                        obj.SetPosition(gateReverse.Positions[random.Next(gateReverse.Positions.Count - 1)]);
                    }

                }
        }
        public void Clear()
        {
            gateNormal.Clear();
            gateReverse.Clear();
        }
        #endregion 

        #region Other Function 
        //Thiết lập loại gate ngẩu nhiên
        public virtual void Add(Vector2 position)
        {
            int temp = random.Next(Constant.PORTRAL_REVERSE);
            if (first) { temp = Constant.PORTRAL_REVERSE; first = false; }
            switch (temp)
            {
                case (Constant.PORTRAL_NORMAL):
                    gateNormal.AddSprite(new Sprite(
                        textureNormal,
                        new Vector2(Constant.PORTRAL_SIZE_X, Constant.PORTRAL_SIZE_Y),
                        new Vector2(Constant.PORTRAL_SHEET_X, Constant.PORTRAL_SHEET_Y),
                        Constant.RATE_PORTRAL,
                        true), position);
                    break;
                default:
                    gateReverse.AddSprite(new Sprite(
                         textureReverse,
                         new Vector2(Constant.PORTRAL_SIZE_X, Constant.PORTRAL_SIZE_Y),
                         new Vector2(Constant.PORTRAL_SHEET_X, Constant.PORTRAL_SHEET_Y),
                         Constant.RATE_PORTRAL,
                         true), position);
                    break;
            }
        }
        #endregion 
    }
}
